package com.example.term;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.io.FileOutputStream;


public class calculate extends Fragment {
    Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_back,
            btn_9,btn_add,btn_minu,btn_mult,btn_div,btn_dot,btn_equl,btn_c;  //按钮0-9 和按钮+-*/
    TextView txtedit,welcome;       //显示文本框
    String name;
    String password;
    int op;     //操作码
    Double res;
    Double num1=null,num2=null;
    boolean isclick = false;
    public calculate() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_calculate, container, false);

    }
    public void onActivityCreated(@Nullable Bundle   savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        //通过id找到对应的按钮
        txtedit = (TextView) getActivity().findViewById(R.id.txtedit);
        btn_0 = (Button) getActivity().findViewById(R.id.btn_0);
        btn_1 = (Button) getActivity().findViewById(R.id.btn_1);
        btn_2 = (Button) getActivity().findViewById(R.id.btn_2);
        btn_3 = (Button) getActivity().findViewById(R.id.btn_3);
        btn_4 = (Button) getActivity().findViewById(R.id.btn_4);
        btn_5 = (Button) getActivity().findViewById(R.id.btn_5);
        btn_6 = (Button) getActivity().findViewById(R.id.btn_6);
        btn_7 = (Button) getActivity().findViewById(R.id.btn_7);
        btn_8 = (Button) getActivity().findViewById(R.id.btn_8);
        btn_9 = (Button) getActivity().findViewById(R.id.btn_9);
        btn_add = (Button) getActivity().findViewById(R.id.btn_add);
        btn_minu = (Button) getActivity().findViewById(R.id.btn_minu);
        btn_mult = (Button) getActivity().findViewById(R.id.btn_mult);
        btn_div = (Button) getActivity().findViewById(R.id.btn_div);
        btn_dot = (Button)getActivity().findViewById(R.id.btn_dot);
        btn_equl =(Button)getActivity().findViewById(R.id.btn_equal);
        btn_c = (Button)getActivity().findViewById(R.id.btn_c);
//        btn_back = (Button)getActivity().findViewById(R.id.btn_back);
//        welcome = (TextView)getActivity().findViewById(R.id.welcome);
        btn_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str = txtedit.getText().toString();
                str=str+ "0";
                txtedit.setText(str);
            }
        });
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "1";
                txtedit.setText(str1);
            }
        });
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "2";
                txtedit.setText(str1);
            }
        });
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "3";
                txtedit.setText(str1);
            }
        });
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "4";
                txtedit.setText(str1);
            }
        });
        btn_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "5";
                txtedit.setText(str1);
            }
        });
        btn_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "6";
                txtedit.setText(str1);
            }
        });btn_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "7";
                txtedit.setText(str1);
            }
        });btn_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "8";
                txtedit.setText(str1);
            }
        });btn_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isclick){
                    txtedit.setText(null);
                    isclick=false;
                }
                String str1 = txtedit.getText().toString();
                str1=str1+ "9";
                txtedit.setText(str1);
            }
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str10 = txtedit.getText().toString();
                if(str10.equals(null))
                {
                    return;
                }
                if(str10==null||str10=="")
                {

                }
                num1 = Double.valueOf(str10);
                txtedit.setText(null);

                op=0;
            }
        });btn_minu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str11 = txtedit.getText().toString();
                if(str11.equals(null))
                {
                    return;
                }
                if(str11==null||str11=="")
                {

                }

                num1 = Double.valueOf(str11);

                txtedit.setText(null);

                op=1;

            }
        });btn_mult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str12 = txtedit.getText().toString();
                if(str12.equals(null))
                {
                    return;
                }
                if(str12==null||str12=="")
                {

                }
                num1 = Double.valueOf(str12);
                txtedit.setText(null);

                op=2;
            }
        });btn_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str13 = txtedit.getText().toString();
                if(str13.equals(null))
                {
                    return;
                }
                if(str13==null||str13=="")
                {

                }
                num1 = Double.valueOf(str13);
                txtedit.setText(null);

                op=3;

            }
        });btn_dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str14 = txtedit.getText().toString();
                if(str14.equals(null)|| str14=="")
                {
                    return;
                }
                str14=str14+ ".";
                txtedit.setText(str14);
            }
        });
        btn_equl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String strequ = txtedit.getText().toString();
                if(strequ.equals(null)|| strequ=="")
                {
                    return;
                }
                if(num1==null)
                {
                    return;
                }
                num2=Double.valueOf(strequ);
                if(num2==0) {
                    Toast.makeText(getActivity(), "被除数不能为0！！", Toast.LENGTH_SHORT).show();

                }
                if(num2==null)
                {

                }
                switch (op){
                    case 0:
                        res = num1+num2;
                        op = 5;
                        break;
                    case 1:
                        res = num1-num2;
                        op = 5;
                        break;
                    case 2:
                        res = num1*num2;
                        op = 5;
                        break;
                    case 3:

                        res = num1/num2;
                        op = 5;
                        break;
                    default:
                        break;
                }
                txtedit.setText(res.toString());
                isclick = true;
            }
        });
        btn_c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtedit.setText(null);
                num2=null;
                num1=null;

            }
        });

    }

}